package com.jwdf.uda.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.ConcernStatusEvent;
import com.jwdf.uda.event.QuizDetailEvent;
import com.jwdf.uda.ui.adapter.item.QuizDetailItem;
import com.jwdf.uda.ui.adapter.viewholder.QuizConcernViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.QuizDescImageViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.QuizDescTextViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.QuizMainViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.QuizPlanViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.QuizReplyImageViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.QuizReplyTxtViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.QuizTitleTabViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.QuizUserTakeVoteHolder;
import com.jwdf.uda.ui.adapter.viewholder.QuizVoteTipViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.QuizVoteTitleViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.QuizVoteUserViewHolder;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/6.
 */
public class QuizDetailAdapter extends AbsAdapter<QuizDetailItem> {
    int voteCount;
    private ArrayList<String> descImages;
    private QuizDetailItem.QuizTab mCurrentTab = QuizDetailItem.QuizTab.REPLY;
    private List<QuizDetailItem> mItems = new ArrayList<>();
    private List<QuizDetailItem> mItemsTab0 = new ArrayList<>();
    private List<QuizDetailItem> mItemsTab1 = new ArrayList<>();
    private Context mContext;

    public QuizDetailAdapter(Context context) {
        super(context);
        mContext = context;
    }

    public QuizDetailItem.QuizTab getCurrentTab() {
        return mCurrentTab;
    }

    public void setCurrentTab(QuizDetailItem.QuizTab currentTab) {
        mCurrentTab = currentTab;
        if (mCurrentTab == QuizDetailItem.QuizTab.VOTE && mItemsTab1.size() <= 0) {
            EventBusUtil.post(new QuizDetailEvent(QuizDetailEvent.EVENT_REFRESH_VOTE));
        }
        for (int i = 0; i < mItems.size(); i++) {
            QuizDetailItem item = mItems.get(i);
            if (item.itemType == QuizDetailItem.TYPE_TITLE_TAB) {
                item.currentTab = currentTab;
            }
        }
        notifyDataSetChanged();
    }

    public void refreshVoteCount(int votes) {
        for (QuizDetailItem item : mItems) {
            if (item.itemType == QuizDetailItem.TYPE_TITLE_TAB) {
                item.voteCount = votes;
                break;
            }
        }
    }

    public void refreshVotes(PbBean.PbResQuestionVoteInfo voteList) {
        mItemsTab1.clear();
        //投票数标题
        QuizDetailItem item = mItems.get(mItems.size() - 1);
        if (item != null && item.itemType == QuizDetailItem.TYPE_TITLE_TAB) {
            item.voteCount = voteList.getVoteCount();
        }
        List<PbBean.PbBaseVoteOptionFinal> voteOptions = voteList.getVoteOptionInfoList();
        if (voteOptions != null && voteOptions.size() > 0) {
            PbBean.PbSignBaseVoteOptionType userVote = voteList.getIsVote();
            for (int i = 0; i < voteOptions.size(); i++) {
                PbBean.PbBaseVoteOptionFinal option = voteOptions.get(i);
                final int size = voteOptions.size();
                switch (option.getVoteOption()) {
                    case VOTE_OPTION_TYPE_A:
                        addVoteTitleItem(mContext.getString(R.string.vote_plan_title_a), PbBean.PbSignBaseVoteOptionType.VOTE_OPTION_TYPE_A, item.isCurrentUser,
                                PbBean.PbSignBaseVoteOptionType.VOTE_OPTION_TYPE_A == userVote,
                                voteList.getVoterInfoCount(), option.getOptionCount(), i, size);
                        break;
                    case VOTE_OPTION_TYPE_B:
                        addVoteTitleItem(mContext.getString(R.string.vote_plan_title_b), PbBean.PbSignBaseVoteOptionType.VOTE_OPTION_TYPE_B, item.isCurrentUser,
                                PbBean.PbSignBaseVoteOptionType.VOTE_OPTION_TYPE_B == userVote,
                                voteList.getVoterInfoCount(), option.getOptionCount(), i, size);
                        break;
                    case VOTE_OPTION_TYPE_C:
                        addVoteTitleItem(mContext.getString(R.string.vote_plan_title_c), PbBean.PbSignBaseVoteOptionType.VOTE_OPTION_TYPE_C, item.isCurrentUser,
                                PbBean.PbSignBaseVoteOptionType.VOTE_OPTION_TYPE_C == userVote,
                                voteList.getVoterInfoCount(), option.getOptionCount(), i, size);
                        break;
                    case VOTE_OPTION_TYPE_D:
                        addVoteTitleItem(mContext.getString(R.string.vote_plan_title_d), PbBean.PbSignBaseVoteOptionType.VOTE_OPTION_TYPE_D, item.isCurrentUser,
                                PbBean.PbSignBaseVoteOptionType.VOTE_OPTION_TYPE_D == userVote,
                                voteList.getVoterInfoCount(), option.getOptionCount(), i, size);
                        break;
                }
            }
            addVoteTipItem();
        }

        List<PbBean.PbBaseVoterInfo> recentlyVote = voteList.getVoterInfoList();
        if (recentlyVote != null && recentlyVote.size() > 0) {
//            addVoteUserDivider();
            for (int i = 0; i < recentlyVote.size(); i++) {
                PbBean.PbBaseVoterInfo vote = recentlyVote.get(i);
                addVoteItem(vote);
            }
        }
        notifyDataSetChanged();
    }

    private void addVoteTipItem() {
        QuizDetailItem item = new QuizDetailItem(QuizDetailItem.TYPE_VOTE_TIP);
        mItemsTab1.add(item);
    }

//    private void addVoteUserDivider() {
//        QuizDetailItem item = new QuizDetailItem(QuizDetailItem.TYPE_VOTE_USER_DIVIDER);
//        mItemsTab1.add(item);
//    }

    private void addVoteTitleItem(String planName, PbBean.PbSignBaseVoteOptionType optionId, boolean isCurrentUser, boolean isUserVote, int countVotes, int countPlanVote, int index, int count) {
        QuizDetailItem item = new QuizDetailItem(QuizDetailItem.TYPE_VOTE_TITLE);
        item.planName = planName;
        item.isUserVote = isUserVote;
        item.isCurrentUser = isCurrentUser;
        item.optionId = optionId;
        item.voteCount = countVotes;
        item.planVoteCount = countPlanVote;
        item.voteIndex = index;
        item.voteOptionCount = count;
        mItemsTab1.add(item);
    }

    private void addVoteItem(PbBean.PbBaseVoterInfo vote) {
        QuizDetailItem item = new QuizDetailItem(QuizDetailItem.TYPE_VOTE);
        item.vote = vote;
        mItemsTab1.add(item);
    }

    public void showUserTakeVote(int voteCount) {
        mItemsTab1.clear();
        QuizDetailItem item = new QuizDetailItem(QuizDetailItem.TYPE_TAKE_VOTE);
        item.voteOptionCount = voteCount;
        mItemsTab1.add(item);
        notifyDataSetChanged();
    }

    public int getCommentOffset() {
        return mItemsTab0.size();
    }

    public int getVoteOffset() {
        int size = mItemsTab1.size() - voteCount - 1;
        return size;
    }

    public void setDetailData(PbBean.PbResQuestionFindQuestion detail) {
        mItems.clear();
        mItemsTab0.clear();
        mItemsTab1.clear();

        addQuizMainInfoItem(detail);
        addQuizMainDescItem(detail.getQuestionInfo());
        if (detail.getQuestionInfo().getVoteOptionCount() > 0) {
//            addQuizMainDividerItem();
            List<PbBean.PbBaseResVoteOption> voteOptions = detail.getQuestionInfo().getVoteOptionList();
            for (int i = 0; i < voteOptions.size(); i++) {
                PbBean.PbBaseResVoteOption voteOption = voteOptions.get(i);
                final int size = voteOptions.size();
                addVotePlanItem(voteOption, i, size);
                setVoteCount(size);
            }
        }

        addQuizConcernItem(detail);
        addQuizTitleTabItem(detail);
        List<PbBean.PbBaseQuestionAnswer> replys = detail.getAnswerInfoList();
        if (replys != null && replys.size() > 0) {
            for (int i = 0; i < replys.size(); i++) {
                PbBean.PbBaseQuestionAnswer reply = replys.get(i);
                addQuizReplyItem(reply);
            }
        }
        notifyDataSetChanged();
    }

    private void addQuizMainInfoItem(PbBean.PbResQuestionFindQuestion detail) {
        QuizDetailItem item = new QuizDetailItem(QuizDetailItem.TYPE_QUIZ_MAIN);
//        item.userInfo = detail.getQuestionInfo().getUserInfo();
        item.quizTitle = detail.getQuestionInfo().getTitle();
        mItems.add(item);
    }

    private void addVotePlanItem(PbBean.PbBaseResVoteOption voteOption, int index, int count) {
        QuizDetailItem item = new QuizDetailItem(QuizDetailItem.TYPE_PLAN_IMAGES);
        item.voteOption = voteOption;
        item.voteIndex = index;
        item.voteOptionCount = count;
        mItems.add(item);
    }

    private void addQuizConcernItem(PbBean.PbResQuestionFindQuestion detail) {
        QuizDetailItem item = new QuizDetailItem(QuizDetailItem.TYPE_QUIZ_CONCERN);
        item.userInfo = detail.getQuestionInfo().getUserInfo();
        item.createTime = detail.getQuestionInfo().getCreateTime();
        item.concernCount = detail.getQuestionInfo().getConcernCount();
        item.isConcern = detail.getIsConcern() == QuizDetailItem.CONCERNED_QUIZ;
        mItems.add(item);
    }

    private void addQuizTitleTabItem(PbBean.PbResQuestionFindQuestion detail) {
        QuizDetailItem item = new QuizDetailItem(QuizDetailItem.TYPE_TITLE_TAB);
        item.replyCount = detail.getQuestionInfo().getAnswerCount();
        item.currentTab = mCurrentTab;
        item.sortType = "按热度排序";
        item.isCurrentUser = TextUtils.equals(detail.getQuestionInfo().getUserInfo().getUserId(), ConfigUtils.getUserId(mContext));
        item.voteCount = detail.getQuestionInfo().getVoteCount();
        item.hasVoteOption = detail.getQuestionInfo().getVoteOptionCount() > 0;
        mItems.add(item);
    }

    public QuizDetailItem getTitleTabItem(PbBean.PbResQuestionFindQuestion detail) {
        QuizDetailItem item = new QuizDetailItem(QuizDetailItem.TYPE_TITLE_TAB);
        item.replyCount = detail.getQuestionInfo().getAnswerCount();
        item.currentTab = mCurrentTab;
        item.sortType = "按热度排序";
        item.isCurrentUser = TextUtils.equals(detail.getQuestionInfo().getUserInfo().getUserId(), ConfigUtils.getUserId(mContext));
        item.voteCount = detail.getQuestionInfo().getVoteCount();
        item.hasVoteOption = detail.getQuestionInfo().getVoteOptionCount() > 0;
        return item;
    }

    private void addQuizReplyItem(PbBean.PbBaseQuestionAnswer reply) {
        QuizDetailItem item;
        if (reply.getQuickImageInfoList() == null || reply.getQuickImageInfoList().isEmpty()) {
            item = new QuizDetailItem(QuizDetailItem.TYPE_REPLY_TXT);
        } else {
            item = new QuizDetailItem(QuizDetailItem.TYPE_REPLY_IMAGE);
        }
        item.reply = reply;
        item.planVoteCount = getVoteCount();
        mItemsTab0.add(item);
    }

    int getVoteCount() {
        return voteCount;
    }

    void setVoteCount(int voteCount) {
        this.voteCount = voteCount;
    }

    private void addDescImages(PbBean.PbBaseQuestion question) {
        List<PbBean.PbBaseResDescription> descriptions = question.getDescriptionList();
        descImages = new ArrayList<>();
        for (PbBean.PbBaseResDescription imageText : descriptions) {
            if (PbBean.PbSignBaseDescriptionType.DESCRIPTION_TPYE_IMG == imageText.getDescType()) {
                PbBean.PbImage image = imageText.getDescImage();
                descImages.add(image.getUrl());
            }
        }
    }

    private void addQuizMainDescItem(PbBean.PbBaseQuestion question) {
        addDescImages(question);
        List<PbBean.PbBaseResDescription> descriptions = question.getDescriptionList();
        int index = 0;
        for (PbBean.PbBaseResDescription imageText : descriptions) {
            if (PbBean.PbSignBaseDescriptionType.DESCRIPTION_TPYE_TEXT == imageText.getDescType()) {
                QuizDetailItem item = new QuizDetailItem(QuizDetailItem.TYPE_QUIZ_MAIN_TXT);
                item.quizMainDescTxt = StringUtils.filterNullAndTrim(imageText.getDescText());
                mItems.add(item);
            } else {
                PbBean.PbImage image = imageText.getDescImage();
                QuizDetailItem item = new QuizDetailItem(QuizDetailItem.TYPE_QUIZ_MAIN_IMAGE);
                item.quizMainDescImage = image.getUrl();
                item.imageWidth = image.getWidth();
                item.imageHeight = image.getHeight();
                item.imageIndex = index++;
                item.imageUrls = descImages;
                mItems.add(item);
            }
        }
    }

//    private void addQuizMainDividerItem() {
//        QuizDetailItem item = new QuizDetailItem(QuizDetailItem.TYPE_QUIZ_MAIN_DIVIDER);
//        mItems.add(item);
//    }

    public void refreshCommentCount(int count) {
        for (QuizDetailItem item : mItems) {
            if (item.itemType == QuizDetailItem.TYPE_TITLE_TAB) {
                item.replyCount = count;
                break;
            }
        }
    }

    public void refreshComments(List<PbBean.PbBaseQuestionAnswer> comments) {
        mItemsTab0.clear();
        if (comments != null && comments.size() > 0) {
            for (int i = 0; i < comments.size(); i++) {
                PbBean.PbBaseQuestionAnswer comment = comments.get(i);
                addQuizReplyItem(comment);
            }
        }
        notifyDataSetChanged();
    }

    public void loadMoreComments(List<PbBean.PbBaseQuestionAnswer> comments) {
        if (comments != null && comments.size() > 0) {
            for (int i = 0; i < comments.size(); i++) {
                PbBean.PbBaseQuestionAnswer comment = comments.get(i);
                addQuizReplyItem(comment);
            }
        }
        notifyDataSetChanged();
    }

    public void loadMoreVotes(List<PbBean.PbBaseVoterInfo> voters) {
        if (voters != null && voters.size() > 0) {
            for (int i = 0; i < voters.size(); i++) {
                PbBean.PbBaseVoterInfo vote = voters.get(i);
                addVoteItem(vote);
            }
        }
        notifyDataSetChanged();
    }

    public void onEventMainThread(ConcernStatusEvent event) {
        if (event != null) {
            for (QuizDetailItem item : mItems) {
                if (QuizDetailItem.TYPE_QUIZ_CONCERN == item.itemType) {
                    item.isConcern = event.isConcern;
                    item.concernCount = event.concernCount;
                    notifyDataSetChanged();
                    break;
                }
            }
        }
    }

    public void onEventMainThread(QuizDetailEvent event) {
        if (event != null && QuizDetailEvent.EVENT_VOTE_CANCELED == event.getEvent()) {
            for (QuizDetailItem item : mItemsTab1) {
                if (QuizDetailItem.TYPE_TAKE_VOTE == item.itemType) {
                    item.optionId = event.optionId;
                    notifyDataSetChanged();
                    break;
                }
            }
        } else if (event != null && QuizDetailEvent.EVENT_DO_SORT == event.getEvent()) {
            for (QuizDetailItem item : mItems) {
                if (item.itemType == QuizDetailItem.TYPE_TITLE_TAB) {
                    item.sortType = event.getSortType();
                    break;
                }
            }
        }
    }

    @Override
    public int getCount() {
        int size = mCurrentTab == QuizDetailItem.QuizTab.REPLY ? mItemsTab0.size() : mItemsTab1.size();
        return mItems.size() + size;
    }

    @Override
    public QuizDetailItem getItem(int position) {
        int index = position - mItems.size();
        if (index < 0) {
            return mItems.get(position);
        } else {
            if (mCurrentTab == QuizDetailItem.QuizTab.REPLY) {
                return mItemsTab0.get(index);
            } else {
                return mItemsTab1.get(index);
            }
        }
    }

    @Override
    protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        switch (itemType) {
            case QuizDetailItem.TYPE_QUIZ_MAIN:
                return new QuizMainViewHolder(mInflater.inflate(R.layout.item_quiz_main, parent, false));
            case QuizDetailItem.TYPE_QUIZ_MAIN_TXT:
                return new QuizDescTextViewHolder(mInflater.inflate(R.layout.item_quiz_main_desc_txt, parent, false));
            case QuizDetailItem.TYPE_QUIZ_MAIN_IMAGE:
                return new QuizDescImageViewHolder(mInflater.inflate(R.layout.item_quiz_main_desc_image, parent, false));
//            case QuizDetailItem.TYPE_QUIZ_MAIN_DIVIDER:
//                return new QuizMainDividerHolder(mInflater.inflate(R.layout.item_quiz_main_divider, parent, false));
            case QuizDetailItem.TYPE_PLAN_IMAGES:
                return new QuizPlanViewHolder(mInflater.inflate(R.layout.item_quiz_plan, parent, false));
            case QuizDetailItem.TYPE_QUIZ_CONCERN:
                return new QuizConcernViewHolder(mInflater.inflate(R.layout.item_quiz_concern, parent, false));
            case QuizDetailItem.TYPE_TITLE_TAB:
                return new QuizTitleTabViewHolder(mInflater.inflate(R.layout.item_quiz_title_tab, parent, false));
            case QuizDetailItem.TYPE_REPLY_TXT:
                return new QuizReplyTxtViewHolder(mInflater.inflate(R.layout.item_reply_txt, parent, false));
            case QuizDetailItem.TYPE_REPLY_IMAGE:
                return new QuizReplyImageViewHolder(mInflater.inflate(R.layout.item_reply_image, parent, false));
            case QuizDetailItem.TYPE_TAKE_VOTE:
                return new QuizUserTakeVoteHolder(mInflater.inflate(R.layout.item_quiz_take_vote, parent, false));
            case QuizDetailItem.TYPE_VOTE_TITLE:
                return new QuizVoteTitleViewHolder(mInflater.inflate(R.layout.item_vote_title, parent, false));
            case QuizDetailItem.TYPE_VOTE_TIP:
                return new QuizVoteTipViewHolder(mInflater.inflate(R.layout.item_vote_tip, parent, false));
//            case QuizDetailItem.TYPE_VOTE_USER_DIVIDER:
//                return new QuizVoteUserDividerHolder(mInflater.inflate(R.layout.view_head_divider, parent, false));
            case QuizDetailItem.TYPE_VOTE:
                return new QuizVoteUserViewHolder(mInflater.inflate(R.layout.item_vote_user, parent, false));
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).itemType;
    }

    @Override
    public int getViewTypeCount() {
        return 12;
    }

}
